package com.zrgj.manager.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.zrgj.common.response.BaseResponse;
import com.zrgj.common.response.StatusCodeEnum;
import com.zrgj.common.utils.NameUtil;
import com.zrgj.domain.Admin;
import com.zrgj.manager.service.IAdminService;
import com.zrgj.manager.service.IUserService;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;


@RestController
@RequestMapping("/admin")
public class AdminController {
    @Reference
    private IAdminService adminService;
    private FastFileStorageClient client;

    public AdminController(FastFileStorageClient client) {
        this.client = client;
    }

    @Value("${CHINASOFT_IMAGE_URL}")
    private String CHINASOFT_IMAGE_URL;

    /**
     * 图片上传
     * @param pic
     * @return
     */
    @PostMapping(value = "/upload")
    public BaseResponse upload(MultipartFile pic){
        //定义一个完整路径
        String visitURL = null;

        //获取文件名
        String originalFilename = pic.getOriginalFilename();
        //获取文件扩展名
        String suffixName = FilenameUtils.getExtension(originalFilename);
        //获取当前上传文件类型
        Integer flieType = NameUtil.getUploadFlieType(suffixName);
        //获取上传文件的大小
        long fileSize = pic.getSize();


        try {
            //判断文件类型
            if (flieType == 1){
                //上传到FastDFS
                StorePath path = client.uploadFile(pic.getInputStream(), fileSize, suffixName, null);
                String url = path.getFullPath();

                visitURL = CHINASOFT_IMAGE_URL + url;

                return new BaseResponse<>(StatusCodeEnum.SUCCESS,visitURL);
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return new BaseResponse(StatusCodeEnum.FORMAT_NOT_SUPPORT);
    }

    /**
     * 检查用户名重复
     * @param adminUsername
     * @return
     */
    @RequestMapping(value = "/checkName")
    public BaseResponse checkName(String adminUsername){
        Admin admin = adminService.findByName(adminUsername);
        if (admin != null){
            return new BaseResponse(StatusCodeEnum.REPEAT_TO_ADD);
        }
        return new BaseResponse(StatusCodeEnum.SUCCESS);
    }

    /**
     * 登录
     * @return
     */
    @RequestMapping(value = "/login")
    public BaseResponse login(@RequestBody Admin admin, HttpServletResponse response) throws UnknownHostException {
        Admin login = adminService.login(admin);
        if (login == null){
            return new BaseResponse(StatusCodeEnum.LOGIN_ERROR);
        }
        String ip = InetAddress.getLocalHost().getHostAddress();
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd~hh:mm:ss");
        String format = simpleDateFormat.format(date);
        Cookie cookie = new Cookie("ADMIN_NAME", admin.getAdminUsername());
        Cookie cookie1 = new Cookie("TIME", format);
        Cookie cookie2 = new Cookie("IP", ip);
        cookie.setPath("/");
        cookie.setMaxAge(1800);
        cookie1.setPath("/");
        cookie1.setMaxAge(1800);
        cookie2.setPath("/");
        cookie2.setMaxAge(1800);
        response.addCookie(cookie);
        response.addCookie(cookie1);
        response.addCookie(cookie2);

        return new BaseResponse(StatusCodeEnum.SUCCESS);
    }

    /**
     * 注册
     * @param admin
     * @return
     */
    @RequestMapping(value = "/register")
    public BaseResponse register(@RequestBody Admin admin){
        adminService.register(admin);
        return new BaseResponse(StatusCodeEnum.SUCCESS,"login");
    }

    @RequestMapping(value = "/logout")
    public BaseResponse logout(HttpServletResponse response){
        Cookie cookie = new Cookie("ADMIN_NAME", null);
        cookie.setPath("/");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
        return new BaseResponse(StatusCodeEnum.SUCCESS);
    }
}
